﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using Portal.Environment;
using Portal.Language;
using Portal.Interface;

namespace Portal.CDSP
{
    public class FAqDetailWP : WebPart
    {
        public override string WebPartId
        {
            get
            {
                return "WebSite.FAqDetailWP";
            }
        }

        public override string Title
        {
            get
            {
                return GetText("Title", "Hỏi đáp"); 
            }
        }

        public override string WebPartFolder
        {
            get
            {
                return "Portal.CDSP";
            }
        }

        public override ViewCls[] Views
        {
            get
            {
                return new ViewCls[]{
                    new ViewCls("default",GetText("default","Chi tiết hỏi đáp"),1),
                };
            }
        }


        public override string NameSpace
        {
            get
            {
                return "Portal.CDSP";
            }
        }

        public override string ClassName
        {
            get
            {
                return "FAqDetailWP";
            }
        }


        [AjaxPro.AjaxMethod]
        public override string ClientRenderWebPart(string BelongSite, string TabModuleId, string ViewName, string HasPorlet, LoadParamCls loadParamCls)
        {
            try
            {
                if (ViewName.Equals("default"))
                {
                    return ClientRenderWebPart(TabModuleId, HasPorlet);
                }

                return GetText("ViewNotFound", "Không tìm thấy View [" + ViewName + "]");
            }
            catch (Exception ex)
            {
                return ex.Message.ToString();
            }
        }


        public override DataTable ReadingDataService(string belongSite, string ActiveLanguage, ref string IdField, ref string SubjectField)
        {
            IdField = "Id";
            SubjectField = "Subject";
            DataTable dtResult = new DataTable();
            dtResult.Columns.Add("Id");
            dtResult.Columns.Add("Subject");
            dtResult.Rows.Add(new object[]{
                "C341A732-C284-4703-A00D-C6D4CD0C095D","Xem chi tiết hỏi đáp"
            });
            return dtResult;
        }


        private string ClientRenderWebPart(string TabModuleId, string HasPorlet)
        {
            TabModuleCls TabModuleCls = new TabModuleBLL().CreateModelFromXml(TabModuleId);
            if (TabModuleCls == null) throw new Exception("[" + TabModuleId + "] " + GetText("TabModuleIdNull", "not found!"));
            try
            {
                string AdvImageId = TabModuleCls.Id;
                string belongSite = TabModuleCls.belongSite;
                string QId = (string)Environments.Request("QId");
                if (QId == null && System.Web.HttpContext.Current.Request.QueryString["QId"] == null)
                {
                    return GetText("NewsIdNotFound", "Không tìm thấy QuestionId");
                }
                else
                {
                    if (QId == null)
                    {
                        QId = System.Web.HttpContext.Current.Request.QueryString["QId"];
                    }
                    FAskQuestionsCls FQuestionCls = new FAskQuestionsBLL().CreateModelWithShortId(Environments.ActiveLanguage, QId);
                    if (FQuestionCls == null)
                    {
                        return "<div class=cssNewsDeleted>" + GetText("NewsDelete", "Câu hỏi này đã bị xóa hoặc ngừng phát hành! Xin quay lại sau!") + "</div>";
                    }
                    DataSet ds = new DataSet();
                    int MaxNewsRelation = 5;
                    if (TabModuleCls.XMLConfig != null)
                    {
                        ds.ReadXml(new System.IO.StringReader(TabModuleCls.XMLConfig));
                        MaxNewsRelation = int.Parse((string)ds.Tables[0].Rows[0]["MaxNewsRelation"]);
                    }
                    string Html = Environments.GetExtraStyleSheet(TabModuleCls);

                    #region old
                    //GroupNewsItemCls GroupNewsItemCls = new CMCSalesGroupNewsBLL().CreateModelItemAlias(NewsCls.GroupNewsItemId);
                    //DataTable dtGroup = new CMCSalesGroupNewsBLL().ReadingGroupNewsItemName(GroupNewsItemCls.ParentGroupNewsItemId);
                    /*if (dtGroup.Rows.Count == 0)
                    {
                        DataTable jdtGroup = new CMCSalesGroupNewsBLL().ReadingMenu(GroupNewsItemCls.GroupNewsId);
                        MenuName = (string)jdtGroup.Rows[0]["" + TabModuleCls.belongSite + "Subject"];
                        menuUrl = Environments.GetNewsCategoryUrl(TabModuleCls.belongSite, GroupNewsItemCls.GroupNewsItemId, GroupNewsItemCls.GroupNewsItemName, "_runtime");
                    }
                    else
                    {
                        MenuName = (string)dtGroup.Rows[0]["" + TabModuleCls.belongSite + "GroupNewsItemName"];
                        menuUrl = (string)dtGroup.Rows[0]["JumpUrl"];
                    }*/

                    #endregion

                    Html +=
                   "<div id=\"leftsub\">" +
                    "<div class=\"leftsubHeader\"><h2>"
                        + TabModuleCls.Title + 
                        "</h2>"+
                    "</div>" +
                    "<div class=\"leftsubContent\"><h3>" + GetText("Question", "Câu hỏi:") + "</h3></div>" +
                    "<div class=artilecontent>" +
                            "<b>" + FQuestionCls.AskContent + "</b>" +
                            "<div class=author>" + GetText("Author", "Độc giả:") + "<b>" + FQuestionCls.AskBy + "</b></div>" +
                            "<div class=articleinfo>" +
                                "<span class=datepost>" + GetText("DatePost", "Ngày hỏi:") + "" + FQuestionCls.AskTime.ToString("dd/MM/yyyy") + 
                                ",Đã có " + FQuestionCls.AskTotalClick.ToString() + " lần đọc</span>" +
                            "</div>" +
                    "<div class=articletitle><h3><i>" + GetText("Answer", "Trả lời:") + "</i></h3></div>" +
                    "<div class=\"artilecontent\">"  +FQuestionCls.AskAnswer + "</div><br>" +
                    " <div class=author><b>" + FQuestionCls.AnswerBy + "</b> " + GetText("Asw", "Trả lời") + "</div><br>" +
                    "<div class=tool><a class=back href=\"javascript:history.go(-1)\"><img src=images/back.gif /></a></div><br>";
                    Html +=
                            "<script language=javascript>" +
                            " function ClientJumpPageNews(TabModuleId,MaxItems, ItemsPerRow, NoOfVisiblePage, CurrentPage, JumpJavaScript)" +
                            "  {" +
                            "      document.getElementById(\"divProcessing\").innerHTML = Portal.CDSP.FAqDetailWP.GetProcessingText().value;" +
                            "      setTimeout(\"RealJumpPageNews('\"+TabModuleId+\"',\"+MaxItems+\",\"+ItemsPerRow+\",\"+NoOfVisiblePage+\",\"+CurrentPage+\",'\"+JumpJavaScript+\"')\",10);" +
                            "  }" +
                            "  function RealJumpPageNews(TabModuleId,MaxItems, ItemsPerRow, NoOfVisiblePage, CurrentPage, JumpJavaScript)" +
                            "  {" +
                            "      PageIndex = CurrentPage;" +
                            "      belongSite=document.getElementById(\"txtBelongSite\").value;" +
                            "      retValue=Portal.CDSP.FAqDetailWP.GetHtml(TabModuleId,belongSite,ItemsPerRow,NoOfVisiblePage,CurrentPage,JumpJavaScript).value;" +
                            "      if(retValue.indexOf(\"ERROR\")!=-1)" +
                            "      {" +
                            "          alert(retValue);" +
                            "          return;" +
                            "      }" +
                            "      document.getElementById(\"divProcessing\").innerHTML=\"\";" +
                            "      document.getElementById(\"related\").innerHTML=retValue;" +
                            "  }" +
                            "</script>" +
                    "<div id =\"related\" class=\"related\">" +
                        GetHtml(TabModuleId, belongSite, 10, 10, 0, "ClientJumpPageNews") +
                    "</div> " +     // End Related 
                    "</div>" +
                    "</div><div class=\"clear\"></div>";
                    return Html;
                }
            }
            catch (Exception ex) {
                return "ERROR:" + ex.Message;
            }
        }


        [AjaxPro.AjaxMethod()]
        public string GetHtml(string TabModuleId,
            string belongSite,            
            int ItemsPerRow,
            int NoOfVisiblePage,
            int CurrentPage,
            string JumpJavaScript)
        {            
            string Html = string.Empty;
            DataTable dtQuestion = new FAskQuestionsBLL().Reading();
            TabModuleCls TabModuleCls = new TabModuleBLL().CreateModelFromXml(TabModuleId);
            if (TabModuleCls == null) throw new Exception("[" + TabModuleId + "] " + GetText("TabModuleIdNull", "not found!"));
            int StartIndex = 0;
            int EndIndex = 0;
            string PagingText = new CDSPPaging().GetPaging(TabModuleId, belongSite, dtQuestion.Rows.Count, ItemsPerRow, NoOfVisiblePage, CurrentPage, JumpJavaScript, ref StartIndex, ref EndIndex);
            Html =
                Environments.GetExtraStyleSheet(TabModuleCls) +
                "<input type=hidden id=txtBelongSite value=\"" + belongSite + "\">" +
                "<div id = divProcessing></div>" +
                "<div class=\"leftcolContent\">" +
                 "<h3>" + GetText("Related", "Câu hỏi khác") + "</h3>" +
                "<ul>";              
                for (int iNews = StartIndex; iNews < EndIndex; iNews++)
                {
                    string ShortId = (string)dtQuestion.Rows[iNews]["ShortId"];

                    string Subject = "";
                    try
                    {
                        Subject = (string)dtQuestion.Rows[iNews]["AskContent"];
                    }
                    catch { }

                    DateTime EntryDate = DateTime.Now;
                    try
                    {
                        EntryDate = (DateTime)dtQuestion.Rows[iNews]["AskTime"];
                    }
                    catch { }
                    string Url = "Question.aspx?QID=" + ShortId; //Environments.GetNewsUrl(TabModuleCls.belongSite, ShortId, Subject, "_runtime");
                    Html += "<li><a href=\"" + Url + "\">" + Subject + "</a>(" + EntryDate.ToString("dd/MM/yyyy") + ")</li>";
                }                
                Html += "</ul>"+
                        "</div>"+
                        "<div class=related></div>";
                if (PagingText.Trim().Length > 0)
                {
                    Html += PagingText;
                }
            return Html;
        }

        [AjaxPro.AjaxMethod]
        public string GetProcessingText()
        {
            string Url = Environments.GetActiveThemeUrl() + "/Global/Loading.gif";
            return "<img src=\"" + Url + "\"><a class=cssProcessing>" + GetText("ProcessingText", "Đang đọc dữ liệu...") + "</a>";
        }



        public override bool AllowFrontService
        {
            get
            {
                return true;
            }
        }

        public override void RegAjaxPro(System.Web.UI.Page ActivePage)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(Portal.CDSP.FAqDetailWP), ActivePage);
        }
    }
}
